package com.ossjk.oa.accounting.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.vo.PagePreNextVo;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.entity.CwReceivables;
import com.ossjk.oa.accounting.mapper.CwCapitalsMapper;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.accounting.service.ICwReceivablesService;
import com.ossjk.oa.accounting.vo.CwCapitalsVo;
import com.ossjk.oa.accounting.vo.MoneySumVo;
import com.ossjk.oa.accounting.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@Service
public class CwCapitalsServiceImpl extends ServiceImpl<CwCapitalsMapper, CwCapitals> implements ICwCapitalsService {
	@Autowired
	private ICwReceivablesService iCwReceivablesService;
	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public PagePreNextVo selectPreOrNextByMth(String midMth ) {
		
		PagePreNextVo vo = new PagePreNextVo();
		
		vo.setPre(this.baseMapper.selectPreMth(midMth));
		vo.setNext(this.baseMapper.selectNextMth(midMth)); 
		vo.setCurrent(midMth);
		//取得6个月以前
 
		vo.setSelectAll( this.baseMapper.selectMax12Mth( CommonMethod.getxxMthBefore(midMth,-6)     ) );
	 
		return vo;
	}
	
    public int countByMth(String mth ) {
        return  baseMapper.countByMth(mth).intValue();
    }

	@Override
	public List<CwCapitalsVo> selectCapitalsByMth(String mth) {
		return baseMapper.selectAllByMth(mth);
	}

	@Override
	public List<CwCapitalsVo> selectCapitalsByTypeC(String typec) {
		return baseMapper.selectAllByTypeC(typec);
	}

	@Override
	public List<StatisticsVo> statistics(Integer type, String m1, String m2) {
		
       if(type == 1) {
    	   return baseMapper.statistics1(m1,m2);
       }else  if(type == 2) {
    	   return baseMapper.statistics2(m1);
       }else   {
    	   return baseMapper.statistics3(m1);
       }

	}

	@Override
	public List<MoneySumVo> selectSumGBBank(String mth) {

		return   baseMapper.selectSumGBBank(mth);
	}
	public boolean cwUpdateCapitals(String id,String needrecDate) {
		System.out.println(needrecDate);
		CwCapitals cw_capitals = selectById(id);
		cw_capitals.setFlag(1);
		if (updateById(cw_capitals)) {

			if (cw_capitals.getTypec().equals("应收款项")) {
				CwReceivables cwReceivables = new CwReceivables();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				if (cw_capitals.getCptacount().compareTo(new BigDecimal(0)) == -1) {
					try {
						cwReceivables.setPayid(cw_capitals.getId());
						cwReceivables.setNeedrecDate(simpleDateFormat.parse(needrecDate));
						cwReceivables.setReceivables(cw_capitals.getCptacount().multiply(new BigDecimal(-1)));
						cwReceivables.setDescb(cw_capitals.getDescb());
						cwReceivables.setFlag(0);
						iCwReceivablesService.insert(cwReceivables);
//						System.out.println(22222);
					} catch (ParseException e) {
						e.printStackTrace();
					}


				}

			}
			return true;
		} else {
			return false;
		}
	}


}
